<template>
    <div>
        <van-nav-bar
                :fixed="true"
                @click-left="onClickLeft"
        >
            <div class="van-nav-bar_title" slot="title">{{ $t('msgCenter.title') }}</div>
            <van-icon name="arrow-left" slot="left" class="nav_bar_icon"></van-icon>
        </van-nav-bar>
        <van-row  class="main">
            <van-overlay :show="msgLoading"></van-overlay>
            <van-loading v-if="msgLoading" type="spinner" class="loading"  vertical></van-loading>
            <van-pull-refresh
                    v-if="list.length>0 && !msgLoading"
                    v-model="isLoading"
                    @refresh="onRefresh"
                    :pulling-text="$t('msgCenter.pulling')"
                    :loosing-text="$t('msgCenter.loosing')"
                    :loading-text="$t('msgCenter.loading')"
            >
                <van-list
                        class="item"
                        v-model="loading"
                        :finished="finished"
                        :finished-text="list.length>=10 ? $t('msgCenter.finished') : ''"
                        :loading-text="$t('msgCenter.loading')"
                        @load="onLoad"
                        @click="toDetail(item)"
                >
                    <van-row
                            v-for="(item,index) in list"
                            :key="index"
                    >
                        <van-row class="time">
                            {{toStr(item['createTime'])}}
                        </van-row>
                        <van-row class="content">
                            <van-col span="24" class="title">
                                {{item['title']}}
                            </van-col>
                            <van-col span="24" class="text">
                                {{item['text']}}
                            </van-col>
                        </van-row>
                    </van-row>
                </van-list>
            </van-pull-refresh>
            <van-row class="noMsg" v-if="list.length===0 && !msgLoading">
                <van-image :src="url" class="img"></van-image>
                <p>{{$t('msgCenter.no')}}</p>
            </van-row>
        </van-row>
    </div>
</template>

<script>
    import { getMsg, readMsg } from '@/api/index'
    import url from "@/assets/imgs/msg.png"
    import {formatDate} from '../libs/date'
    export default {
        name: "message-center",
        data(){
            return{
                url:url,
                isLoading:false,
                list: [],
                nextId: null,
                loading: false,
                msgLoading: true,
                finished: false
            }
        },
        mounted(){
            this.getInfo();
        },
        methods:{
            getData(){
                let data={
                    nextId: this.nextId,
                    limit:10
                };
                getMsg(data).then(res => {
                    this.loading=false;
                    this.msgLoading=false;
                    if (res.data.success) {
                        this.nextId = res.data.data["nextId"] && res.data.data["nextId"]>0 ? res.data.data["nextId"] : null;
                        if(res.data.data["appPushHistoryOutputBOList"]){
                            this.list = res.data.data["appPushHistoryOutputBOList"];
                        }
                    } else {
                        this.$notify(res.data.message);
                    }
                })
            },
            onClickLeft(){
                this.$bridge.goBack();
            },
            onRefresh(){
                this.finished=false;
                let data={
                    nextId: null,
                    limit:10
                };
                getMsg(data).then(res => {
                    this.isLoading=false;
                    if (res.data.success) {
                        this.nextId = res.data.data["nextId"] && res.data.data["nextId"]>0 ? res.data.data["nextId"] : null;
                        if(res.data.data["appPushHistoryOutputBOList"]){
                            this.list = res.data.data["appPushHistoryOutputBOList"];
                        }
                    } else {
                        this.$notify(res.data.message);
                    }
                })
            },
            getNextData(){
                if(this.nextId){
                    let data={
                        nextId: this.nextId,
                        limit:10
                    };
                    getMsg(data).then(res => {
                        this.loading=false;
                        if (res.data.success) {
                            this.nextId = res.data.data["nextId"] && res.data.data["nextId"]>0 ? res.data.data["nextId"] : null;
                            if(res.data.data["appPushHistoryOutputBOList"]){
                                this.list = this.list.concat(
                                    res.data.data["appPushHistoryOutputBOList"]
                                );
                            }
                        } else {
                            this.$notify(res.data.message);
                        }
                    })
                }else{
                    this.loading=false;
                    this.finished=true;
                }
            },
            onLoad() {
                this.getNextData();
            },
            toStr(data) {
                return formatDate(new Date(data), 'dd-MM-yyyy hh:mm');
            },
            getInfo(){
                this.take().then(data => {
                    if(data){
                        console.log(data)
                        window.sessionStorage.setItem("loginInfo", JSON.stringify(data));
                        this.getData();
                        readMsg();
                    }else{
                        this.$bridge.goLogin()
                    }
                });
            },
            take(){
                return new Promise((resolve,reject)=>{
                    this.$bridge["getLoginInfo"](null, res => {
                        resolve(JSON.parse(res));
                    });
                });
            },
            toDetail(item){
                localStorage.setItem('info', JSON.stringify(item))
                this.$router.push({
                    path: 'message-info'
                })
            }
        }
    }
</script>

<style scoped lang="less">
    @import "../assets/css/main";
    .van-pull-refresh{
        min-height: calc(100vh - 56px);
    }
    .item{
        background: #ffffff;
        padding: 16px;
        margin-bottom: 8px;
        .time{
            color: #999999;
            text-align: center;
            font-size: 12px;
            .content{
                padding-top: 16px;
                .title{
                    color: #2D3632;
                    font-size: 16px;
                    line-height: 24px;
                    padding-bottom: 8px;
                }
                .text{
                    font-size: 14px;
                    color: #999999;
                    line-height: 20px;
                }
            }
        }
    }
    .noMsg{
        padding-top: 177px;
        color: #999999;
        font-size: 14px;
        letter-spacing: 1px;
        text-align: center;
        .img{
            width: 134px;
            height: 90px;
        }
    }

    .loading{
        z-index: 1000;
        position: absolute;
        margin-top: 60%;
        margin-left: 45%;
    }
</style>